package us.versus.them.triangle_mountain;

class MidPointBuddy {

	var midPoint_map : Hash< Pt3 >;
	var f : Float;

	public function new( f ) {
		this.f = f;
		this.midPoint_map = new Hash< Pt3 >();
	}

	public function getMidPoint( a:Pt3, b:Pt3, max:Float ){
		var s = a.toString() + "/" + b.toString();
		var mid = this.midPoint_map.get( s ); /* this blows up */

		if ( null == mid ) {
			s = b.toString() + "/" + a.toString();
			mid = midPoint_map.get( s );
			if ( null == mid ) {

				mid = new Pt3( 
					  ( b.x - a.x ) / 2 + a.x
					, ( b.y - a.y ) / 2 + a.y 
					, ( b.z - a.z ) / 2 + a.z
				);

				// this shows why starting with 2 trianbles to make a square instead of just
				// one triangle is a good idea... :-D
				if ( f == mid.x || -f == mid.x || f == mid.z || -f == mid.z ) {
					if ( mid.y > 0 ) mid.y = 0;
				} else {
					mid.y += Math.random() * max - Math.random() * max;
				}
				midPoint_map.set( s , mid );
			}
		}
		return mid;
	}

}
